package net.j4love.spring.mvc.ex.async;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

/**
 * @author He Peng
 * @create 2017-06-29 18:22
 * @update 2017-06-29 18:22
 * @see
 */

@Controller
@RequestMapping(
        path = "/async"
)
public class AsyncController {

    @RequestMapping(path = "/logic-view")
    public Callable<String> asyncHandlerRetLogicView() throws ExecutionException, InterruptedException {

        return new Callable<String>() {
            public String call() throws Exception {
                // ...
                return "async";
            }
        };
    }

    @RequestMapping(path = "/mav")
    public Callable<ModelAndView> asyncHandlerRetModelAndView() throws ExecutionException, InterruptedException {

        final Thread mainThread = Thread.currentThread();
        final Date startTime = new Date();
        return new Callable<ModelAndView>() {
            public ModelAndView call() throws Exception {
                Thread.sleep(3000);
                ModelAndView mav = new ModelAndView("async");
                mav.addObject("startTime",startTime);
                mav.addObject("completeTime",new Date());
                mav.addObject("mainThread",mainThread);
                mav.addObject("asyncThread",Thread.currentThread());
                return mav;
            }
        };
    }
}
